
/******************************************************************************
 * 
 * 
 * A filter receive message and decide (thank to the filtre methode) 
 * if a message have to be keep on not.
 * If the filter have a validator, the massage have to get into the validator too
 * 
 * @author Mathieu Maret
 *****************************************************************************/
public abstract class Filter {
	
	Validator validator = null;
	boolean enableValidator = true;

	/**
	 * Create a new Filter with a validator
	 * @param v a validator associated to the filter
	 */
	public Filter(Validator v){
		validator = v;
	}
	/**
	 * Create a new filter without filter
	 */
	public Filter(){
		validator = null;
	}
	
	/**
	 * @param m
	 * @return return true is the message have succefully past into the filter
	 */
	public abstract boolean filtre(Message m);
	
	/**
	 * @param m
	 * @return check if the message is conforme to the filter and to the validator (if it exist)
	 */
	public boolean isCorrect(Message m){
		if(validator == null){
			return this.filtre(m);
		}else{
			boolean responce =  this.filtre(m);
			if(enableValidator && responce){
				responce = validator.isValidate(m);
			}
			return responce;
		}
	}
	
	/**
	 * @return return the validator associated with the filter
	 */
	public Validator getValidator() {
		return validator;
	}
	

	/**
	 * @param validator
	 */
	public void setValidator(Validator validator) {
		this.validator = validator;
	}
	
	public void remValidator(){
		this.validator = null;
	}
	
	
	public boolean isEnableValidator() {
		return enableValidator;
	}
	/**
	 * A Validator could be turn on/off.
	 * Is the validator is turned off, the message won't be passed to this validator is the filtering succed
	 * @param enableValidator
	 */
	public void setEnableValidator(boolean enableValidator) {
		this.enableValidator = enableValidator;
	}
	

}
